package com.douxg.fastCoding.web.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.douxg.fastCoding.enjoy.EnjoyManager;
import com.douxg.fastCoding.spring.consts.BizCode;
import com.douxg.fastCoding.spring.consts.ElementMsgType;
import com.douxg.fastCoding.spring.vo.Result;
import com.douxg.fastCoding.web.dto.EnjoyDto;
import com.douxg.fastCoding.web.dto.InOutDv;
import com.douxg.fastCoding.web.service.EnjoyService;
import com.douxg.fastCoding.web.vo.EnjoyVo;
import com.jfinal.kit.Kv;
import com.jfinal.template.Template;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description
 * @Auther
 * @Date 2020-07-31 15:06
 */
@RequestMapping("fc/enjoy")
@RestController
public class EnjoyController {
    @Resource
    private EnjoyService service;
    @Resource
    private EnjoyManager enjoyManager;

    @PostMapping("insert")
    public Result insert(@RequestBody EnjoyDto enjoyDto) {
        boolean handleResult = service.insert(enjoyDto);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("update")
    public Result update(@RequestBody EnjoyDto enjoyDto) {
        boolean handleResult = service.updateById(enjoyDto);
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("delete")
    public Result delete(@RequestBody EnjoyDto enjoyDto) {
        boolean handleResult = service.removeById(enjoyDto.getId());
        if (handleResult) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("page")
    public Result page(@RequestBody EnjoyDto enjoyDto, Integer current, Integer size) {
        IPage<EnjoyVo> projectPage = service.page(enjoyDto, current, size);
        return Result.success(projectPage);
    }

    @PostMapping("selectOne")
    public Result selectOne(@RequestBody EnjoyDto enjoyDto) {
        EnjoyVo enjoyVo = service.selectOne(enjoyDto);
        return Result.success(enjoyVo);
    }

    @PostMapping("selectFunctionTypes")
    public Result selectFunctionTypes(String functionName) {
        JSONObject jsonObject = service.selectFunctionTypes(functionName);
        return Result.success(jsonObject);
    }

    @PostMapping("handle")
    public Result handle(@RequestBody InOutDv inOutDv) {
        String content = inOutDv.getContent();
        if (StrUtil.isEmpty(content)) {
            return Result.create(BizCode.PARAM_ISNULL);
        }
        String input = inOutDv.getInput();
        Kv kv = Kv.by("in", input);

        Template template = enjoyManager.getTemplateByContent(content);
        String result = template.renderToString(kv);
        return Result.success(result);
    }

    @PostMapping("renew")
    public Result renew(@RequestBody EnjoyDto enjoyDto) {
        int i = service.renew(enjoyDto);
        if (i == -1) {
            return Result.error();
        }
        if (i == 1) {
            return Result.success();
        }
        return Result.success().setType(ElementMsgType.warning.name()).setMsg("无可恢复模板");
    }

    @PostMapping("allRenew")
    public Result allRenew() {
        int i = service.allRenew();
        if (i < 0) {
            return Result.error().setMsg("失败数量为：" + (-i));
        }
        if (i == 1) {
            return Result.success();
        }
        return Result.success().setType(ElementMsgType.warning.name()).setMsg("无可恢复模板");
    }
}
